September 04, 2021
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
selected = [0] * (m)
def solve(x):
if x == m + 1:
for s in selected:
print(s, end=' ')
print()
else:
for candidate in range(1, n + 1):
used = False
for idx in range(x):
if candidate == selected[idx]:
used = True
if not used:
selected[x - 1] = candidate
solve(x + 1)
selected[x - 1] = 0
solve(1)
import sys
n, m = map(int, sys.stdin.readline().split(' '))
selected = [0 for _ in range(m)]
used = [0 for _ in range(n + 1)]
def rec_func(k):
if k == m:
for x in selected:
sys.stdout.write(str(x) + ' ')
sys.stdout.write('\n')
else:
for cand in range(1, n + 1):
# used 배열로 시간 복잡도 줄이기
if used[cand]:
continue
selected[k] = cand
used[cand] = 1
rec_func(k + 1)
selected[k] = 0
# 실수하기 쉬운 부분, 재귀호출 후에 초기화해줘야 함
used[cand] = 0
rec_func(0)